package com.tansuo365.test1.controller.othercontrol;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tansuo365.test1.bean.otherbean.DWuliao;
import com.tansuo365.test1.service.kucunservice.WuliaoKucunSearService;
import com.tansuo365.test1.service.otherservice.DWuliaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/dwuliao")
public class DWuliaoController {

    @Autowired
    private DWuliaoService dWuliaoService;
    @Autowired
    private WuliaoKucunSearService wuliaoKucunSearService;

    @RequestMapping("/getSelective")
    public Map<String,Object> getDWuliao(DWuliao dWuliao,
                                         @RequestParam(value="page",required=true)Integer page,
                                         @RequestParam(value="rows",required=true)Integer rows){

        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(page, rows);
        List<DWuliao> byWaiId = dWuliaoService.findSelective(dWuliao);
        PageInfo<DWuliao> pageInfo = new PageInfo<DWuliao>(byWaiId);
        resultMap.put("rows", pageInfo.getList());
        resultMap.put("total", pageInfo.getTotal());
        return resultMap;
    }

    @RequestMapping("/save")
    public Map<String, Object> save(HttpSession session, DWuliao dWuliao) {
        Map<String, Object> resultMap = new HashMap<>();
        dWuliao.setFounder((String) session.getAttribute("truename"));
        int code = dWuliaoService.insertSelective(dWuliao);
        List<DWuliao> list = new ArrayList<>();
        list.add(dWuliao);
        int kucunSearCode = wuliaoKucunSearService.insertWuliaoToKucunWhenWuliaoBiao(list);
        if (code > 0 && code==kucunSearCode) {
            resultMap.put("success", true);
        } else {
            resultMap.put("success", false);
        }
        return resultMap;
    }

    @RequestMapping("/update")
    public Map<String, Object> update(DWuliao dWuliao) {
        Map<String, Object> resultMap = new HashMap<>();
        int code = dWuliaoService.updateByPrimaryKeySelective(dWuliao);
        if (code > 0) {
            resultMap.put("success", true);
        } else {
            resultMap.put("success", false);
        }
        return resultMap;
    }

    @RequestMapping("/delete")
    public Map<String, Object> delete(@RequestParam("ids[]") Integer[] ids) {
        Map<String, Object> resultMap = new HashMap<>();
        if(ids.length==0){
            resultMap.put("success",false);
            resultMap.put("errorInfo","请选中条目进行删除");
            return resultMap;
        }
        int code = dWuliaoService.deleteBatchByPKArr(ids);
        if (code > 0) {
            resultMap.put("success", true);
        } else {
            resultMap.put("success", false);
        }
        return resultMap;
    }

}
